package com.summer.safe.utils;


/**
 * 通用的模板生成引擎
 *
 */
public class SimpleTemplateEngine extends TemplateEngine {


    public SimpleTemplateEngine(GenerateConfig config) {
        super(config);
    }


    /**
     * List页面
     */
    @Override
    protected void generatePageListHtml() {
        String htmlPath = htmlPath() + ToolUtil.firstLetterToLower(super.config.getGen().getClassName()) + "List.html";
        generateFile("templates/pageList.html.btl", htmlPath);
        System.out.println("生成list页面成功!路径:" + htmlPath);
    }


    /**
     * Form页面
     */
    @Override
    protected void generatePageFormHtml() {
        String htmlPath = htmlPath() + ToolUtil.firstLetterToLower(super.config.getGen().getClassName()) + "Form.html";
        generateFile("templates/pageForm.html.btl", htmlPath);
        System.out.println("生成form页面成功!路径:" + htmlPath);
    }

    /**
     * View页面
     */
    @Override
    protected void generatePageViewHtml() {
        String htmlPath = htmlPath() + ToolUtil.firstLetterToLower(super.config.getGen().getClassName()) + "View.html";
        generateFile("templates/pageView.html.btl", htmlPath);
        System.out.println("生成view页面成功!路径:" + htmlPath);
    }

    /**
     * Controller层
     */
    @Override
    protected void generateController() {
        String controllerPath = javaPath(0) + super.config.getGen().getClassName() + "Controller.java";
        generateFile("templates/Controller.java.btl", controllerPath);
        System.out.println("生成controller成功!路径:" + controllerPath);
    }

    /**
     * Dao层
     */
    @Override
    protected void generateDao() {
        String daoPath = javaPath(1) + super.config.getGen().getClassName() + "Dao.java";
        generateFile("templates/Dao.java.btl", daoPath);
        System.out.println("生成Dao成功!路径:" + daoPath);
    }

    /**
     * Service层
     */
    @Override
    protected void generateService() {
        String servicePath = javaPath(2) + super.config.getGen().getClassName() + "Service.java";
        generateFile("templates/Service.java.btl", servicePath);
        System.out.println("生成Service成功!路径:" + servicePath);
    }

    /**
     * Entity层
     */
    @Override
    protected void generateEntity() {
        String entityPath = javaPath(3) + super.config.getGen().getClassName() + ".java";
        generateFile("templates/Entity.java.btl", entityPath);
        System.out.println("生成Entity成功!路径:" + entityPath);
    }

    /**
     * Mapping
     */
    @Override
    protected void generateXml() {
        String xmlPath = xmlPath() + super.config.getGen().getClassName() + "Dao.xml";
        generateFile("templates/Mapping.xml.btl", xmlPath);
        System.out.println("生成Mapping成功!路径:" + xmlPath);
    }


    private String getPackagePath() {
        String[] packages = super.config.getScheme().getPackageName().split("\\.");
        StringBuilder packagePath = new StringBuilder();
        for (String aPackage : packages) {
            packagePath.append(aPackage).append("/");
        }
        return packagePath.toString();
    }

    private String javaPath(int type) {
        StringBuilder sb = new StringBuilder();
        sb.append(super.config.getProjectPath());
        sb.append("/src/main/java/");
        sb.append(getPackagePath());
        sb.append(super.config.getScheme().getModuleName() + "/");
        if (ToolUtil.isNotEmpty(super.config.getScheme().getSubModuleName())) {
            sb.append(super.config.getScheme().getSubModuleName() + "/");
        }
        switch (type) {
            case 0:
                sb.append("controller/");
                break;
            case 1:
                sb.append("dao/");
                break;
            case 2:
                sb.append("service/");
                break;
            case 3:
                sb.append("entity/");
                break;
            default:
        }

        return sb.toString();
    }

    private String xmlPath() {
        StringBuilder sb = new StringBuilder();
        sb.append(super.config.getProjectPath());
        sb.append("/src/main/resources/");
        sb.append("mapper/");
        /*sb.append(super.config.getScheme().getModuleName() + "/");
        if (ToolUtil.isNotEmpty(super.config.getScheme().getSubModuleName())) {
            sb.append(super.config.getScheme().getSubModuleName() + "/");
        }*/
        return sb.toString();
    }

    private String jsPath() {
        StringBuilder sb = new StringBuilder();
        sb.append(super.config.getProjectPath());
        sb.append("/src/main/webapp/static/modules/");
        return sb.toString();
    }

    private String htmlPath() {
        StringBuilder sb = new StringBuilder();
        sb.append(super.config.getProjectPath());
        sb.append("/src/main/webapp/WEB-INF/view/");
        sb.append(super.config.getScheme().getModuleName() + "/");
        if (ToolUtil.isNotEmpty(super.config.getScheme().getSubModuleName())) {
            sb.append(super.config.getScheme().getSubModuleName() + "/");
        }
        return sb.toString();
    }
}
